install_or_load_pack <- function(pack){
  
  create.pkg <- pack[!(pack %in% installed.packages()[, "Package"])]
  
  if (length(create.pkg))
    
    install.packages(create.pkg, dependencies = TRUE)
  
  sapply(pack, require, character.only = TRUE)
  
}

packages <- c('ggplot2', 'dplyr', 'latex2exp', 'ggpubr', 
              'stats', 'leqslv', 'VGAM', 'MASS', 'parallel', 
              'plm', 'lqmm', 'tidyverse', 'readstata13')

install_or_load_pack(packages)

# Load main functions 
source('functions.R')
source('utils.R')

# Generate Figure 3 & 4 
source('fig3_fig4.R')

# Fig 5a & Fig 6 
source('applications/homeowner_comment.R')

# Fig 5b & Fig A4
source('application/bureacrat_aaa.R')

# Fig A2a & Fig A2b
source('DGP II/simulations.R')

# Fig A1
source('Smith 11/smith_simulations.R')

# Generate Fig A4

B <- function(alpha){
  T <- sqrt(2)*erf(2*(1-alpha) - 1, inv = T)
  return(1/((1 - alpha) + sqrt(2)*exp(-T^2/2)/(2*T*sqrt(pi))))
}

alphas <- seq(0, 0.8, length.out = 100)


figA5 <- ggplot() + geom_line(aes(y = abs(B(alphas)), x = alphas)) + 
  labs(x = TeX('$\\alpha_2'), y =  '|1/B|') + theme_bw() 

ggsave(figA5, file = 'plots/figA5.pdf', width = 4, height = 3)


